package com.curry.apiprovider.service;

import com.curry.apiprovider.crawler.Constants;
import com.curry.apiprovider.generator.mapper.TCityInfoMapper;
import com.curry.apiprovider.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

@Service
public class QuartzOperatorService {

    private Logger logger = LoggerFactory.getLogger(QuartzOperatorService.class);
    @Autowired
    private CrawlerWrapperService crawlerWrapperService;
    //
    @Autowired
    private TCityInfoMapper tCityInfoMapper;


    public void startTask(Class<?> clazz, Map<String, Object> params) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Set<String> className = StringUtils.getClassName("com.curry.apiprovider.service.quartz", false);
        if (className != null) {
            for (String classObj : className) {
                System.out.print("classObj " + classObj);
                Class<?> aClass = Class.forName(classObj);
                String name = aClass.getName();
                String clazzName = clazz.getName();
                if (clazzName.equals(name)) {
                    Constructor<?> constructor = aClass.getConstructor(TCityInfoMapper.class, CrawlerWrapperService.class);
                    Object instance = constructor.newInstance(tCityInfoMapper, crawlerWrapperService);
                    Method method = aClass.getMethod("executeTask", Map.class);
                    method.invoke(instance, params);
                }
            }
        }
    }
}

